package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbCustomerDao;
import com.woniuxy.entitys.TbCustomer;
import com.woniuxy.entitys.TbCustomerExample;
import com.woniuxy.services.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private TbCustomerDao tbCustomerDao;
    @Override
    public PageInfo<TbCustomer> getAllCustomers(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbCustomer> list = tbCustomerDao.selectAll();
        PageInfo<TbCustomer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TbCustomer> getOneByTel(Integer pageNum, Integer pageSize, String telphoneCondition) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbCustomer> list = tbCustomerDao.selOneByTel(telphoneCondition);
        PageInfo<TbCustomer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TbCustomer> getByCondition(Integer pageNum, Integer pageSize, Date beginTime, Date endTime ,Integer radio) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbCustomer> list = tbCustomerDao.selByCondition(beginTime,endTime,radio);
        PageInfo<TbCustomer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TbCustomer> getByRadio(Integer pageNum, Integer pageSize, Integer radio) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbCustomer> list = tbCustomerDao.selByRadio(radio);
        PageInfo<TbCustomer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public TbCustomer getDetailByCid(Integer cid) {
        return tbCustomerDao.selectByPrimaryKey(cid);
    }

    @Override
    public List<TbCustomer> selectAll() {
        return tbCustomerDao.selectAll();
    }

    @Override
    public TbCustomer getCustomerInfo(String phone) {
        TbCustomerExample tbCustomerExample = new TbCustomerExample();
        TbCustomerExample.Criteria criteria = tbCustomerExample.createCriteria();
        criteria.andPhoneEqualTo(phone);
        return tbCustomerDao.selectByExample(tbCustomerExample).get(0);
    }

    @Override
    public boolean modifyInfo(TbCustomer tbCustomer) {
        return tbCustomerDao.updateByPrimaryKeySelective(tbCustomer) > 0;
    }
}
